package com.yingsheng.bi.hks.service.addr;

import com.yingsheng.bi.hks.basic.PUser;
import com.yingsheng.bi.hks.basic.utils.Subject;
import com.yingsheng.bi.hks.bo.AddrBo;
import com.yingsheng.bi.hks.dto.addr.*;
import com.yingsheng.bi.hks.mapping.AddrMgmtMappings;
import com.yingsheng.bi.hks.repository.dao.po.Address;
import com.yingsheng.bi.hks.repository.dao.service.AddressDaoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class AddrMgmtService {

    @Autowired
    private AddressDaoService addressDaoService;

    /**
     * 收件信息新增
     * @param req
     * @return
     */
    public AddrAddRes add(AddrAddReq req) {
        PUser user = Subject.get();
        Address address = AddrMgmtMappings.INSTANCE.addReq2po(req);
        address.setUserId(user.getId());
        addressDaoService.insert(address);
        return new AddrAddRes();
    }

    public AddrModRes mod(AddrModReq req) {
        PUser user = Subject.get();
        Address address = AddrMgmtMappings.INSTANCE.addReq2po(req);
        address.setUserId(user.getId());
        addressDaoService.updateByAddrId(address);
        return new AddrModRes();
    }

    public AddrQryRes qry(AddrQryReq req) {
        PUser user = Subject.get();
        List<Address> list = addressDaoService.query(user.getId(),req.getType());
        List<AddrBo> addrList = AddrMgmtMappings.INSTANCE.addrList2boList(list);
        AddrQryRes res = new AddrQryRes();
        res.setAddrBoList(addrList);
        return res;
    }

    public AddrDelRes del(AddrDelReq req) {
        addressDaoService.del(req.getId());
        AddrDelRes res = new AddrDelRes();
        return res;
    }
}
